// component/my-goods-water/my-goods-water.js
const util = require('../../utils/util.js')
const App = getApp()
Component({
  options: {
    styleIsolation: 'apply-shared'
  },
  /**
   * 组件的属性列表
   */
  properties: {
    data: Object
  },
  /**
   * 组件的初始数据
   */
  data: {
    addShoppingCart: null, // 防抖中转
    isLogin: false
  },

  lifetimes: {
    attached() {
      this.setData({
        addShoppingCart: util.debounce(this.addShopping, 500)
      })
      this.isLogin()
    }
  },
  pageLifetimes: {
    show() {
      this.isLogin()
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    addcart(e) {
      this.data.addShoppingCart(e)
    },
    // 添加到购物车
    async addShopping() {
      let eventData = arguments[0]
      let productInfo = eventData.currentTarget.dataset.item,
        list_id = ''

      if (util.isLogin() !== true) {
        return false
      }
      await util
        .request(
          '/api/goods/details',
          {
            id: productInfo.id
          },
          'GET'
        )
        .then((response) => {
          list_id = response.data.data.list[0].id
        })

      var params = {
        goods_id: productInfo.id,
        list_id: list_id,
        goods_number: 1
      }
      if (params.goods_id == '' || params.goods_id == undefined) {
        wx.showToast({
          title: '请选择完整商品规格',
          icon: 'error',
          duration: 1000 //持续的时间
        })
      } else if (params.list_id == '' || params.list_id == undefined) {
        wx.showToast({
          title: '请选择商品规格',
          icon: 'error',
          duration: 1000 //持续的时间
        })
      } else {
        util.request('/api/cart/addcart', params, 'POST').then((res) => {
          const reslut = res.data
          if (reslut.code == 0) {
            const data = reslut.data
            const goods = {
              goods_id: data.goodsid,
              goods_number: data.total,
              id: data.id,
              select: 1
            }
            try {
              let cart = wx.getStorageSync('cart')
              if (!cart) {
                cart = []
                cart.push(goods)
              } else {
                const addGoods = cart.find((item) => item.goods_id == data.goodsid)
                if (!addGoods) {
                  cart.unshift(goods)
                } else {
                  addGoods.goods_number = data.total
                }
              }

              wx.setStorageSync('cart', cart)
              util.setCartCount(cart)
            } catch (e) {
              console.log(e)
            }

            //设置购物车下标
            return util.showtoast(
              {
                msg: res.data.msg,
                icon: 'none'
              },
              1000
            )
          }
          util.showtoast(
            {
              msg: res.data.msg,
              icon: 'error'
            },
            1000
          )
        })
      }
    },
    isLogin() {
      if (!this.data.isLogin) {
        const token = wx.getStorageSync('token')
        if (token) {
          const isLogin = true
          if (this.data.isLogin !== isLogin) {
            this.setData({ isLogin })
          }
        }
      }
    }
  }
})
